package com.tianji.aigc.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.aigc.Service.IChatSessionService;
import com.tianji.aigc.config.SessionProperties;
import com.tianji.aigc.domain.ChatSession;
import com.tianji.aigc.domain.VO.SessionVO;
import com.tianji.aigc.mapper.ChatSessionMapper;
import com.tianji.common.utils.RandomUtils;
import com.tianji.common.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatSession>  implements IChatSessionService {
    @Autowired
    private SessionProperties sessionProperties;
    @Override
    public SessionVO createSession(Integer n) {

        //思路 将数据保存到数据库中
        //查询 从vo获取数据

        Long userId = UserContext.getUser();
        String sessionId = IdUtil.fastSimpleUUID();

        ChatSession chatSession = ChatSession.builder()
                //uuid随机生成SessionId
                .sessionId(sessionId)
                .userId(userId)
                .build();
        this.save(chatSession);

        SessionVO bean = BeanUtil.toBean(sessionProperties, SessionVO.class);
        bean.setSessionId(sessionId);
        bean.setExamples(RandomUtils.randomEleList(sessionProperties.getExamples(), n));
        return bean;

    }

    @Override
    public List<SessionVO.Example> getHotSession(Integer n) {
        return RandomUtils.randomEleList(sessionProperties.getExamples(), n);

    }
}
